var express = require('express');
var bodyParser  =  require("body-parser");  
var app = express();

// need it...  
app.use(bodyParser.urlencoded({ extended: false }));  


app.get('/', function (req, res) {
  res.send('Hello World!');
});
app.get('/info', function (req, res) {
	
  console.log('jj')
  res.setHeader("Access-Control-Allow-Origin", "*");

  res.type('application/json');
  var info;
  console.log(req.query)
  if(req.query.id == 1){
	  info = {name:'admin',age:16}
  }else{
	  info = {name:'fangke',age:'fangke'}
  }
  
  
  res.json(info);
});
app.get('/infos/:id', function (req, res) {
	
  console.log(req.params.id)
  res.setHeader("Access-Control-Allow-Origin", "*");

  var info = {name:'fangke',age:'fangke'}

  
  res.json(info);
});
app.post('/infos/:id', function (req, res) {
	
  
  res.setHeader("Access-Control-Allow-Origin", "*");
	
  if(req.params.id==1){
	  console.log('success')
		  var newInfo = {
		  name:req.body.name,
		  age:req.body.age
	  }
  }
  var info = [];
  
	
  info.push(newInfo)
  
  res.json(info);
});
app.get('/messageboard', function (req, res) {
 
  res.setHeader("Access-Control-Allow-Origin", "*");

  res.type('application/json');
  
  var info = [
	{name:'lileo',msg:'how are you doing',time:'2015-02-12'},
	{name:'hanmeimei',msg:'Nice to meet you~~',time:'2015-05-12'},
	{name:'Tom',msg:'where are you going this Monday~',time:'2016-04-12'}
  ];

  res.json(info);
});
app.get('/search', function (req, res) {
 
  res.setHeader("Access-Control-Allow-Origin", "*");

  res.type('application/json');
  
  var info = [
      {
        article_one:[ 
            {
                "imgUrl": "img/图层 65.png",
                "title": "荣耀4C",
                "content": "至强性能&nbsp;冰点价格"
            },
            {
               "imgUrl": "img/图层 63.png",
                "title": "苹果iphone6splus",
                "content": "给你新鲜体验"
            },
            {
                "imgUrl": "img/图层 64.png",
                "title": "meitu手机",
                "content": "女孩子的最爱"
            },
            {
                "imgUrl": "img/图层 66.png",
                "title": "魅族蓝色经典",
                "content": "给你不一样的色彩感受"
            },
            {
                "imgUrl": "img/图层 67.png",
                "title": "nuoio 2.0",
                "content": "全新品牌概念手机"
            },
            {
                "imgUrl": "img/图层 68.png",
                "title": "自拍神器",
                "content": "即是手机也是相机"
            }
        ]
      },
      {
        article_two:[
        {name:'网络写手',msg:'img/调用专用图片/0.png',time:'<p>阿双方尽快撒哈哈撒发哈时间发货款时间发货撒放假哈时间反馈函数</p>'},
	    {name:'90后作家',msg:'img/调用专用图片/1.png',time:'<p>阿双方尽快撒哈哈撒发哈时间发货款时间发货撒放假哈时间反馈函数</p>'},
	    {name:'洛克菲',msg:'img/调用专用图片/2.png',time:'<p>阿双方尽快撒哈哈撒发哈时间发货款时间发货撒放假哈时间反馈函数</p>'}
        ]
      }
  ];
  var newInfo = info.filter(function(x){
	  return x.name === req.query.name && x;
  })
  res.json(newInfo);
});
app.get('/welcome', function (req, res) {
  //var name = req.query.name;

  //res.setHeader("Access-Control-Allow-Origin", "*");
  //res.type('application/json');
  
  //res.send('welcome, '+ name);
  
  var name = req.query.name,age = req.query.age;

  //res.setHeader("Access-Control-Allow-Origin", "*");
  //res.type('application/json');
  
  res.send('I am, '+ name + ' I\'m ' + age + ' years old');
});
app.get('/getHtml', function (req, res) {

  res.setHeader("Access-Control-Allow-Origin", "*");
  res.type('.html');
 
  res.sendFile(__dirname + '/jqueryAjax-copy.html');
});
app.get('/getJson', function (req, res) {

  //res.setHeader("Access-Control-Allow-Origin", "*");
 
  res.sendFile(__dirname + '/j.js');
});
app.post('/saveInfo', function (req, res) {
	//console.log('saveInfo')
   res.type('application/json');
   res.setHeader("Access-Control-Allow-Origin", "*");

  console.log(req.body)
  var name = req.body.name,
    	age = req.body.age;
	console.log(name + ' ' + age)
  var json = {
	  name:name,
	  age:age
  }
  res.json(json);
});
app.get('/vit',function  (req,res,next) {
    res.type('application/json');
    res.jsonp({'name':'马啊','age':'34','hobby':'eat'});  //items is the object
});
app.listen(3030,'172.16.24.232', function () {
  console.log('Example app listening on port 3030!');
});